package Concurrency.basic.cooperation;

/**
 * 协作例子：异步结果
 * ExecuteThread：执行子线程
 */

import java.util.concurrent.Callable;

public class ExecuteThread<V> extends Thread{
    private V result = null;
    private Exception exception = null;
    private boolean done = false;
    private Callable<V> task;
    private Object lock;
    public ExecuteThread(Callable<V> task, Object lock) {
       this.task = task;
       this.lock = lock;
    }

    @Override
    public void run() {
       try {
           System.out.println("ExecuteThread run is running....");
           result = task.call();
       } catch (Exception e) {
           exception = e;
       } finally {
           synchronized (lock) {
               System.out.println("ExecuteThread run finish....");
               done = true;
               lock.notifyAll();
           }
       }
    }

    public V getResult() {
        return result;
    }

    public boolean isDone() {
        return done;
    }

    public Exception getException() {
        return exception;
    }
}
